<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB PHP | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB PHP</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在php中使用mongodb必须使用 mongodb 的 php驱动。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MongoDB PHP在各平台上的安装及驱动包下载请查看:<a href="https://hcldirgit.github.io/2017/08/27/MongoDB/7.%20MongoDBPHP%20%E6%89%A9%E5%B1%95/" target="_blank" rel="external">PHP安装MongoDB扩展驱动</a></p>
<p>如果使用的是 PHP7，请参阅：<a href="https://hcldirgit.github.io/2017/08/27/MongoDB/9.%20MongoDB%20PHP7/" target="_blank" rel="external">MongoDB PHP7</a></p>
<h2 id="确保连接及选择一个数据库"><a href="#确保连接及选择一个数据库" class="headerlink" title="确保连接及选择一个数据库"></a>确保连接及选择一个数据库</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了确保正确连接，需要指定数据库名，如果数据库在mongoDB中不存在，mongoDB会自动创建</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接默认主机和端口为：mongodb://localhost:27017</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 获取名称为 "test" 的数据库</span></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h2 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建集合的代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 获取名称为 "test" 的数据库</span></div><div class="line">$collection = $db-&gt;createCollection(<span class="string">"runoob"</span>);</div><div class="line"><span class="keyword">echo</span> <span class="string">"集合创建成功"</span>;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行以上程序，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">集合创建成功</div></pre></td></tr></table></figure>
<h2 id="插入文档"><a href="#插入文档" class="headerlink" title="插入文档"></a>插入文档</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在mongoDB中使用 insert() 方法插入文档：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;插入文档代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接到mongodb</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 选择一个数据库</span></div><div class="line">$collection = $db-&gt;runoob; <span class="comment">// 选择集合</span></div><div class="line">$document = <span class="keyword">array</span>( </div><div class="line">        <span class="string">"title"</span> =&gt; <span class="string">"MongoDB"</span>, </div><div class="line">        <span class="string">"description"</span> =&gt; <span class="string">"database"</span>, </div><div class="line">        <span class="string">"likes"</span> =&gt; <span class="number">100</span>,</div><div class="line">        <span class="string">"url"</span> =&gt; <span class="string">"http://www.runoob.com/mongodb/"</span>,</div><div class="line">        <span class="string">"by"</span>, <span class="string">"菜鸟教程"</span></div><div class="line">);</div><div class="line">$collection-&gt;insert($document);</div><div class="line"><span class="keyword">echo</span> <span class="string">"数据插入成功"</span>;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行以上程序，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">数据插入成功</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在 mongo 客户端使用 db.runoob.find().pretty(); 命令查看数据：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20PHP/01.jpeg?raw=true" alt=""></figure></p>
<h2 id="查找文档"><a href="#查找文档" class="headerlink" title="查找文档"></a>查找文档</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用find() 方法来读取集合中的文档。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;读取使用文档的代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接到mongodb</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 选择一个数据库</span></div><div class="line">$collection = $db-&gt;runoob; <span class="comment">// 选择集合</span></div><div class="line"></div><div class="line">$cursor = $collection-&gt;find();</div><div class="line"><span class="comment">// 迭代显示文档标题</span></div><div class="line"><span class="keyword">foreach</span> ($cursor <span class="keyword">as</span> $document) &#123;</div><div class="line">    <span class="keyword">echo</span> $document[<span class="string">"title"</span>] . <span class="string">"\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行以上程序，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">MongoDB</div></pre></td></tr></table></figure>
<h2 id="更新文档"><a href="#更新文档" class="headerlink" title="更新文档"></a>更新文档</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 update() 方法来更新文档。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例将更新文档中的标题为’ MongoDB 教程’， 代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;pre&gt;</div><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接到mongodb</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 选择一个数据库</span></div><div class="line">$collection = $db-&gt;runoob; <span class="comment">// 选择集合</span></div><div class="line"><span class="comment">// 更新文档</span></div><div class="line">$collection-&gt;update(<span class="keyword">array</span>(<span class="string">"title"</span>=&gt;<span class="string">"MongoDB"</span>), <span class="keyword">array</span>(<span class="string">'$set'</span>=&gt;<span class="keyword">array</span>(<span class="string">"title"</span>=&gt;<span class="string">"MongoDB 教程"</span>)));</div><div class="line"><span class="comment">// 显示更新后的文档</span></div><div class="line">$cursor = $collection-&gt;find();</div><div class="line"><span class="comment">// 循环显示文档标题</span></div><div class="line"><span class="keyword">foreach</span> ($cursor <span class="keyword">as</span> $document) &#123;</div><div class="line">    <span class="keyword">echo</span> $document[<span class="string">"title"</span>] . <span class="string">"\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行以上程序，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">MongoDB 教程</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在 mongo 客户端使用 db.runoob.find().pretty(); 命令查看数据：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20PHP/02.jpeg?raw=true" alt=""></figure></p>
<h2 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 remove() 方法来删除文档。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例中将移除 ‘title’ 为 ‘MongoDB 教程’ 的一条数据记录。， 代码片段如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$m = <span class="keyword">new</span> MongoClient(); <span class="comment">// 连接到mongodb</span></div><div class="line">$db = $m-&gt;test; <span class="comment">// 选择一个数据库</span></div><div class="line">$collection = $db-&gt;runoob; <span class="comment">// 选择集合</span></div><div class="line"></div><div class="line"><span class="comment">// 移除文档</span></div><div class="line">$collection-&gt;remove(<span class="keyword">array</span>(<span class="string">"title"</span>=&gt;<span class="string">"MongoDB 教程"</span>), <span class="keyword">array</span>(<span class="string">"justOne"</span> =&gt; <span class="keyword">true</span>));</div><div class="line"></div><div class="line"><span class="comment">// 显示可用文档数据</span></div><div class="line">$cursor = $collection-&gt;find();</div><div class="line"><span class="keyword">foreach</span> ($cursor <span class="keyword">as</span> $document) &#123;</div><div class="line">    <span class="keyword">echo</span> $document[<span class="string">"title"</span>] . <span class="string">"\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除了以上实例外，在php中还可以使用findOne(), save(), limit(), skip(), sort()等方法来操作Mongodb数据库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更多的操作方法可以参考 Mongodb 核心类：<a href="http://php.net/manual/zh/mongo.core.php。" target="_blank" rel="external">http://php.net/manual/zh/mongo.core.php。</a></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/8. MongoDB PHP/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
